<?php
namespace app\im\model;

use app\BaseModel;
use think\facade\Db;
class ImChat extends BaseModel
{
    //定义表名称
    protected $name = 'longbing_card_chat';
    //获取用户信息
//    public function customer()
//    {
//        return $this->hasOne('ImCustomer' ,)
//    }
    //创建
    public function createChat($data) 
    {
        $data['create_time'] = time();
        $result = $this->save($data);
        if(!empty($result)) $result = $this->id;
        return $result;
    } 
    
    //更新    
    public function updateChat($filter ,$data) 
    {
        $filter['deleted'] = 0;
        return $this->updateRow($filter ,$data);
    }
    
    //获取列表
    public function listChat($user_id , $uniacid,$page_config)
    {





        $start_row = 0;
        $page_count = 10;
        if(isset($page_config['page_count']) && !empty($page_config['page_count']) && $page_config['page_count'] > 0) $page_count = $page_config['page_count'];
        if(isset($page_config['page']) && !empty($page_config['page']) && $page_config['page'] > 0) $start_row = ($page_config['page'] -1) * $page_count;
        $result = $this->where(function ($query) use ($user_id) {
            $query->whereOr(['target_id' => $user_id , 'user_id' => $user_id]) ;
        })
            ->where(['deleted' => 0 ,'uniacid' => $uniacid])
            ->order('update_time', 'desc')
            ->limit($start_row,$page_count)
            ->select();

//        $result = $this->where(['deleted' => 0])
//            ->where(function($query) use($user_id) {
//                $query->
//            })
//            ->limit($start_row,$page_count)
//            ->select();

        if(!empty($result)) $result = $result->toArray();
        return $result;
    }
    
    //获取总数
    public function listChatCount($user_id , $uniacid)
    {
        return $this->where(function ($query) use ($user_id) {
            $query->whereOr(['target_id' => $user_id , 'user_id' => $user_id]) ;
            })
            ->where(['deleted' => 0 ,'uniacid' => $uniacid])
            ->count();
    }
    //获取所有
    public function listChatAll($user_id ,$uniacid) 
    {
        $result = $this->where(function ($query) use ($user_id) {
            $query->whereOr(['target_id' => $user_id] , ['user_id' => $user_id]) ;
        })
            ->where(['deleted' => 0 ,'uniacid' => $uniacid])
            ->field('id as chat_id,user_id,target_id')
            ->select();

        if(!empty($result)) $result = $result->toArray();
        return $result;
    }
    //获取单个Chat
    public function getChat($user_id ,$target_id ,$uniacid)
    {
        //查询条件1
        $where = [
            ['user_id' , '=' ,$user_id],
            ['target_id' ,'=' , $target_id],
        ];
        $whereOr = [
            ['user_id' , '=' ,$target_id],
            ['target_id' ,'=' , $user_id],
        ];
        $result = $this->where(function ($query) use($where, $whereOr){
            $query->whereOr([$where,$whereOr]);
        })
            ->where(['uniacid' => $uniacid ,'deleted' => 0])
            ->field('id as chat_id,user_id,target_id')
            ->find();
        return $result;
    }
    //通过id获取chat
    public function getChatById($chat_id)
    {
        $result = $this->where(['id' => $chat_id])->find();
        if(!empty($result)) $result = $result->toArray();
        return $result;
    }
    //删除
    public function delChat($filter)
    {
        $filter['deleted'] = 0;
        return $this->updateChat($filter ,['deleted' => 0]);
    }
    //真删除
    public function destroyChat($filter)
    {
        return $this->destroyRow($filter);
    }
}
